<!doctype html>
<html>

<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<link rel="stylesheet" href="../res/css/weui.css" />
	<link rel="stylesheet" href="../res/css/weuix.css" />
	<link rel="stylesheet" href="../res/css/common.css" />
	<script src="../res/js/tim/trtc.js"></script>
	<script src="../res/js/zepto.min.js"></script>
	<script src="../res/js/zepto.weui.js"></script>
	<script src="../res/js/common.js?v=1"></script>
	<script src="../res/js/socket.js"></script>
	<!-- <script src="inquiry_chat.js?v=13"></script> -->

	<!-- <script src="../res/js/tim/purl.js"></script>
	<script src="../res/js/tim/vconsole.min.js"></script>
	<script src="../res/js/tim/GenerateTestUserSig.js"></script>
	<script src="../res/js/tim/lib-generate-test-usersig.min.js"></script>
	<script src="../res/js/tim/imBase.js"></script> -->
	<!-- <script src="../res/js/socket.js"></script> -->
	<script>
		var log_id, isOnline, onlineNum, hospital_id, log_type, doctor_id, doctor_imid, userInfo;
		onlineNum = 0;
		var strOpenId = getQst("openid"),
			oldDoctorName;

		//获取医生状态信息
		function getDoctStatus() {
			var paramJson = {
				"id": doctor_id,
			};
			newAjaxPost("/h5/doctorInfo/getDoctorInfo", paramJson, getDoctStatusOk);
			return false;
		}

		function getDoctStatusOk(jsonInfo) {
			if (jsonInfo.code == '1') {
				var data = jsonInfo.data;
				userInfo = data
				isOnline = data.isOnline;
				oldDoctorName = data.trueName
				$('#doctorImg').attr("src", data.doctorHeadImg)
				$('#doctorName').html(data.trueName)
				$('#tag').html(data.doctorJobTitle)
				return
				//增加判断获取到医生掉线3次后弹出医生掉线提醒
				if (onlineNum >= 3) {
					//提示医生掉线，取消排队
					$.confirm('医生掉线已掉线，确定要退出吗？', '提示', function () {
						cancelWait();
					});
				}
				if (data.is_online != "1") {
					onlineNum++;
					setTimeout(function () {
						getDoctStatus();
					}, 2000);
				} else {
					onlineNum = 0;
					setTimeout(function () {
						getDoctStatus();
					}, 3000);
				}
				//增加判断获取到医生掉线3次后弹出医生掉线提醒
			} else
				msgInfo(jsonInfo.info);
		}

		//获取排队信息
		function getWaitUser() {
			// console.log(dataType.addPatientQueue);
			parent.wsConnection.send(parent.dataType.addPatientQueue, {
				logId: log_id, // 本次问诊的id
				doctorImId: doctor_imid // 医生id
			});
			setTimeout(function () {
				getWaitUser();
			}, 10000);
		}
		parent.wsConnection.onMessage = function (msg) {
			// todo 判断数据包类型进行后续处理 dataType
			console.log(msg);
			if (msg.packageType == 9 || msg.packageType == 23) { //医生离线信息
				$.confirm("医生已掉线，请重新问诊", "提示", function () {
					$.toast("取消排队成功!");
					parent.wsConnection.send(parent.dataType.cancelPatientQueue, {
						logId: log_id, // 本次问诊的id
					});
					outOfIFrame('inquiry_doctor_index.htm');
				}, function () {
					//取消操作
				});
			} else if (msg.packageType == 6) { //接收排队信息
				var num = msg.list.length
				var waitlist = ''
				for (var i = 0; i < num; i++) {
					if (log_id == msg.list[i].logId) {
						userInfo = msg.list[i];
						// userInfo
						// imId: 74					// 患者IM
						// inquiryPatientId: 264	// 问诊房间号id
						// logId: 305				// 问诊id
						waitlist += '<div class="info_content"><div class="info_isonline">' + msg.list[i].nickname +
							'</div><div class="info_com vg">我</div><div class="info_time">' + msg.list[i].createTime +
							'</div></div>'
					} else {
						waitlist += '<div class="info_content"><div class="info_isonline">' + msg.list[i].nickname +
							'</div><div class="info_time">' + msg.list[i].createTime + '</div></div>'
					}
				}
				$('.wait_list').html(waitlist);
			} else if (msg.packageType == 7) { //医生接诊跳转到问诊页面
				// console.log(userInfo);
				if (msg.inquiryPatientInfoResp.id == userInfo.inquiryPatientId && msg.inquiryPatientInfoResp.patientIM ==
					userInfo.imId) { // inquiryPatientId问诊房间号id	imId患者IM
					sessionStorage.setItem('TRTCset', jsonToStr(msg.tencentConfig))
					//医生接诊
					if(getQst('log_type') == 1){
						$.modal({
							title: '视频问诊温馨提示',
							text: '您的服务医生已接诊，请您点击下方确认按钮进入问诊室进行视频问诊',
							buttons: [{
									text: "确定",
									onClick: function () {
										outOfIFrame(`chat.htm?
										isChatWait=0&doctor_imid=${doctor_imid}&log_id=${log_id}&doctor_id=${doctor_id}&log_type=${log_type}&
										inquirypatientId=${userInfo.inquiryPatientId}&patientIM=${ userInfo.imId}&tvflag=${getQst("tvflag")}`)
									},
								}
							],
						});
					}else{
						outOfIFrame(`chat.htm?
						isChatWait=0&doctor_imid=${doctor_imid}&log_id=${log_id}&doctor_id=${doctor_id}&log_type=${log_type}&
						inquirypatientId=${userInfo.inquiryPatientId}&patientIM=${ userInfo.imId}&tvflag=${getQst("tvflag")}`)
					}
					// goPage(`inquiry_chat.htm?isChatWait=0&doctor_imid=${doctor_imid}&log_id=${log_id}&doctor_id=${doctor_id}&log_type=${log_type}&inquirypatientId=${userInfo.inquiryPatientId}
					// &patientIM=${ userInfo.imId}&tvflag=${getQst("tvflag")}`)
					
				}
			} else if (msg.packageType == 16) { //医生分诊：退费，加跳转到付费页面
				newRefee()
				$.alert({
					title: '提示',
					text: '医生已分诊，当前问诊费会在1-3工作日内原路返回',
					onOK: function () {
						var isIos = checkWxAndIos();
						if (isIos && getQst("log_type") == '1') {
							goPage('inquiry_skip.htm?openid=' + strOpenId)
						} else {
							goPage("inquiry_doctor_index.htm");
						}
						// goPage("inquiry_doctor_index.htm");
					}
				});
			} else if (msg.packageType == 22) { //医生分诊：退费，加跳转到付费页面
				let type = ''
				if (log_type == 1) {
					type = '图文问诊'
				} else {
					type = '视频问诊'
				}
				$.alert({
					title: '提示',
					text: '该医生只支持' + type + '，您可以退诊查费，再重新发起问诊。',
					onOK: function () {
						var isIos = checkWxAndIos();
						if (isIos && getQst("log_type") == '1') {
							goPage('inquiry_skip.htm?openid=' + strOpenId)
						} else {
							goPage("inquiry_doctor_index.htm");
						}
						// goPage("inquiry_doctor_index.htm");
					}
				});
			}
		};

		//取消排队
		function cancelWait() {
			$.confirm("您确定要取消排队吗?", "提示", function () {
				var params = {
					"id": log_id,
					"type": 0
				}
				newAjaxPost("/h5/dectorEndInquiry/unSuccessfulInquiry", params, cancelWaitOk);
				// $.toast("取消排队成功!");
				parent.wsConnection.send(parent.dataType.cancelPatientQueue, {
					logId: log_id, // 本次问诊的id
				});
				// outOfIFrame('inquiry_doctor_index.htm');
			}, function () {
				//取消操作
				pushHistory()
			});
		}

		function cancelWaitOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				$.toast("取消排队成功!");
				parent.wsConnection.send(parent.dataType.cancelPatientQueue, {
					logId: log_id, // 本次问诊的id
				});
				outOfIFrame('inquiry_doctor_index.htm');
			}
		}
		//收到分诊时，退费
		function newRefee() {
			var params = {
				"doctorId": doctor_id,
				"isFrozen": 1
			}
			newAjaxPost("/h5/wxpay/refund", params, newRefeeOk);
		}

		function newRefeeOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				// msgInfo('退费成功，请重新选择医生问诊');
			}
		}

		$(function () {
			//创建websocket连接
			//初始化webscoket
			// function initWebSocket() {
			// 	// 连接socket 重构的聊天要用到socket
			// 	var token = null;
			// 	var userJson = localStorage.getItem('DSXBuserJson');
			// 	user_id = strToJson(userJson).id
			// 	if (userJson) {
			// 		token = strToJson(userJson).h5Token
			// 	}
			// 	wsConnection.initWebSocket('wss://hospital-test.yixianmedical.com/websocket', token);
			// }
			// initWebSocket()

			// checkLogin();
			log_id = getQst("log_id"); //logid问诊号
			log_type = getQst("log_type"); //问诊类型
			doctor_id = getQst("doctor_id"); //医生id
			doctor_imid = getQst("doctor_imid"); //医生im
			getDoctStatus();
			// getWaitUser();
			$('.wait_list').html('正在努力加入排队中，请稍等');
			setTimeout(function () {
				getWaitUser();
			}, 1000);

			//取消排队
			$('#btnWaiting').on("click", function () {
				cancelWait();
			});

			$("#btnBack").on('click', cancelWait)
		});
		
					
		// ================阻止用户直接返回\
		pushHistory();
		window.addEventListener("popstate", cancelWait, false);
		function pushHistory() {
			var state = {
				dataTitle: "title",
				url: "#"
			};
			window.history.pushState(state, "title", "#");
			console.log('pushed')
		}
		// ================阻止用户直接返回/


		function audioAutoPlay(id) {
			var audio = document.getElementById(id),
				play = function () {
					audio.load();
					audio.play();
					document.removeEventListener("touchstart", play, false);
				};
			audio.play();
			document.addEventListener("WeixinJSBridgeReady", function () {
				play();
			}, false);
			document.addEventListener('YixinJSBridgeReady', function () {
				play();
			}, false);
			document.addEventListener("touchstart", play, false);
		}
		// 判断是否ios
		function checkWxAndIos() {
			var u = navigator.userAgent;
			var ua = navigator.userAgent.toLowerCase();
			var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
			if (isIOS)
				return true;
			else
				return false;
		}
	</script>
	<style>
		.header {
			display: flex;
			text-align: center;
			z-index: 1;
			top: 0;
			transition: 0.5s;
			margin: 10px;
			background: white;
			padding: 10px 0;
			border-radius: 10px;
		}

		.doctor-avatar {
			width: 80px;
			height: 80px;
			border: 1px solid #1189ff;
			border-radius: 50%;
			display: inline-block;
			margin-left: 10px;
		}

		#tag {
			display: inline-block;
			justify-content: center;
			font-size: 12px;
			background-color: #ECF6FF;
			color: #1189ff;
			width: 60px;
			border-radius: 5px;
			margin-left: 5px;
			height: 20px;
			margin-top: 31px;
		}

		.wait_footer {
			display: flex;
			justify-content: center;
		}

		.wait_footer>div {
			padding: 0 32px;
			padding-top: 10px;
			text-align: center;
		}

		.wait_footer>div span {
			font-size: 25px;
			color: #404dff;
		}

		#btnWaiting {
			background: #1189ff;
			color: #ffffff;
			font-size: 14px;
			padding-left: 10px;
			padding-right: 10px;
		}

		.wait_list {
			display: block;
		}

		.info_content {
			border-bottom: 1px solid #E0E0E0;
			padding: 10px 0;
			font-size: 14px;
		}

		.info_content:last-child {
			border-bottom: 0px;
		}

		.info_isonline {
			display: inline-block;
		}

		.info_com {
			color: #1189ff;
			background-color: #ECF6FF;
			display: unset;
			padding: 2px 5px;
			border-radius: 7px;
			margin-left: 10px;
			font-size: 12px;
		}

		.info_time {
			color: #353535;
		}

		#btnWaiting {
			height: 30px;
			line-height: 30px;
			margin-top: 25px;
			margin-left: 10px;
		}

		#start {
			color: black;
		}

		#start span {
			color: #1189ff;
		}

		.display-none{
			display: none;
		}
	</style>

<body ontouchstart>
	<div class="weui-header weui-btn_primary">
		<div class="weui-header-left"><a href="javascript:;" id="btnBack" class="icon icon-109"></a></div>
		<div class="weui-header-title">问诊候诊</div>
		<div class="weui-header-right"></div>
	</div>

	<audio src="https://lj.nxk520.com/form_mobile/util/imgs/msg.mp3" id="Jaudio" class="media-audio" preload
		loop="loop"></audio>
	<div class="header">
		<img id="doctorImg" class="doctor-avatar" />
		<div id="doctorName"
			style="font-size:16px;display:inline-block;height: 26px;margin-top: 27px;margin-left: 5px;">暂无</div>
		<div id='tag'>123</div>
		<div id="but">
			<a href="javascript:;" id="btnWaiting" class="weui-btn weui-btn_primary">取消排队</a>
		</div>
	</div>
	<div class="weui-cells" style="margin: 10px; border-radius: 5px; color: #b0afb2; ">
		<div style="text-align: center;">医生兼顾线下坐诊，请耐心等待</div>
		<div class="weui-cell wait_list" style="color: black; font-size: 18px;">
		</div>
		<!-- <div  class="weui-cell" style="justify-content: center;">
            <p >跳出弹框,进入视频</p>
        </div> -->
		<!-- <div class="weui-cell__ft" style="padding: 30px;"><a href="javascript:;" id="btnWaiting" class="weui-btn weui-btn_primary">取消排队</a></div> -->
	</div>
	<!-- 问诊订单查询 -->
	<!-- <div class="weui-mask" style="opacity: 1;"></div>
	<div class="weui-dialog" style="opacity: 1;">
		<div class="expertlist_map feeInfo" style="border-radius: 0px;">
		</div>
		<div class="weui-dialog__bd feeInfoDate">您确定要删除吗?</div>
		<div class="weui-dialog__ft feeInfoSelect">
			<a href="javascript:;" class="weui-dialog__btn default">取消</a>
			<a href="javascript:;" class="weui-dialog__btn primary">确定</a>
		</div>
	</div> -->
</body>

</html>